package com.mtzqc.kt

import android.app.Activity
import com.blankj.utilcode.util.CollectionUtils
import com.mtzqc.img.Engine
import com.luck.picture.lib.basic.PictureSelectionModel
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener


fun Activity.picture(
    mode: Int = SelectMimeType.ofImage()
): PictureSelectionModel {
    return PictureSelector.create(this)
        .openGallery(mode)
        .setImageEngine(Engine.imgEngine())
        .setCompressEngine(Engine.compressEngine())
}

inline fun PictureSelectionModel.onResult(
    crossinline success: (ArrayList<LocalMedia>) -> Unit
) {
    this.forResult(object : OnResultCallbackListener<LocalMedia> {
        override fun onResult(result: ArrayList<LocalMedia>?) {
            if (CollectionUtils.isNotEmpty(result)) {
                success.invoke(result!!)
            }
        }

        override fun onCancel() {

        }
    })
}


inline fun PictureSelectionModel.onSingleResult(
    crossinline success: (LocalMedia) -> Unit
) {
    onResult{
        success.invoke(it.first())
    }
}

fun LocalMedia.resultPath(): String {
    // 例如 LocalMedia 里面返回三种path
    // 1.media.getPath(); 为原图path
    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
    if (isCompressed && !compressPath.isNullOrBlank()) {
        return compressPath;
    }
    if (isCut && !cutPath.isNullOrBlank()) {
        return cutPath
    }
    var ps = path
    if (ps.isNotBlank()) {
        if (!ps.startsWith("content")) {
            return ps
        }
    }
    return realPath;
}
